<?php
    require_once(MODEL_PATH.'/CMS/Base.php');
    class Model_CMS_Member extends MODEL_CMS_Base{
        
        function __construct(){
        }
        
        function basic_info($mid){
          $dbh_r = $this->db_connect('read');
          $h_member = db_selectrow_hash($dbh_r, 'select * from member_imore where id=?', $mid);
          $a_info = array();
          array_push($a_info, "ID: ".$mid);
          array_push($a_info, "username:".$h_member['username']);
          
          $type = db_select_one($dbh_r, 'select cname from member_type where id=?', $h_member['member_type']);
          array_push($a_info, $type);
        	return implode('<BR>', $a_info);
        }
        
        function blacklisted($mid){
        	$dbh_r = $this->db_connect('read');
        	$count = db_select_one($dbh_r, 'select count(*) from member_blacklist where mid=? and status=1', $mid);
        	if ($count == 0){
        		return 0;
        	}
        	return 1;
        }
        
        
        public function related_info($type, $mid, $product=''){

        	if ($type == 'report') return $this->reports($mid, $product);
			if ($type == 'application') return $this->application($mid);
        	
        	return 'No related info';
        }
        
        private function get_application_status($status){
        	$dbh_r =  $this->db_connect('read');
        	return db_select_one($dbh_r, 'select cname from application_status where id=?', $status);
        	
        }
        
        // report written by the user
        function reports($mid, $product=''){

        	$dbh_r =  $this->db_connect('read');
        	
        	$a_result = array();
        	
        	$count=1;
			
			$url = '';
			$ids = '';
			if ($product){
				
				$ids = db_selectcol_array($dbh_r, 'select id from product_reports where oid=? and mid=? and is_publish=1 and status=1', array($product, $mid));
				
			}else{
				$ids = db_selectcol_array($dbh_r, 'select id from product_reports where mid=? and status=1', $mid);
			}

        	foreach ($ids as $id){
        		$h_report = db_selectrow_hash($dbh_r, 'select * from product_reports where id=?', $id);
        		$h_application = db_selectrow_hash($dbh_r, 'select * from product_application where mid=? and oid=?', array($h_report['mid'], $h_report['oid']));
        		
        		
        		$product_cname = db_select_one($dbh_r, 'select cname from product where id=?', $h_report['oid']);
        		
        		
        		$a_detail = array();
        		array_push($a_detail, '<h4 style="padding-margin: 5px;">'.$count.'. Product:'.$product_cname.'</h4>');
				$title = $h_report['title'];
				if($product){
					$url = '/cms/builder/product/reports.php/edit/'.$product.'/'.$h_report['id'];
					$title = html_link($title, $url, array('openwin'=>1));
				}
        		array_push($a_detail, 'Title:'.$title.' (Submitted:'.$h_report['addtime'].')'); 
        		if ($h_application != null){
        		  array_push($a_detail, 'Application Status:'.$this->get_application_status($h_application['application_status'])); 
        		}
        		
        		array_push($a_result, implode("<BR>", $a_detail));
        		$count++;
        	}
        	
        	if (count($a_result) == 0) return '<H3>Never submitted reports.</H3>';
        	$temp = '<H3>Latest Reports:</H3>';
        	return $temp.implode("<BR>", $a_result);
        }
        function application($mid){
        	$dbh_r =  $this->db_connect('read');
        	
        	$a_result = array();
        	
        	$count=1;
			
        	$ids = db_selectcol_array($dbh_r, 'select id from product_application where mid=? and (application_status=7 or application_status=9) and status=1', $mid);
			
        	foreach ($ids as $id){
				$product = db_select_one($dbh_r, 'select oid from product_application where id=? and status=1', $id);

				$h_product = db_selectrow_hash($dbh_r, 'select * from product where id=? and status=1', $product);
				/*
        		$h_report = db_selectrow_hash($dbh_r, 'select * from product_reports where id=?', $id);
        		$h_application = db_selectrow_hash($dbh_r, 'select * from product_application where mid=? and oid=?', array($h_report['mid'], $h_report['oid']));
        		
        		
        		$product = db_select_one($dbh_r, 'select cname from product where id=?', $h_report['oid']);
        		*/
        		
        		$a_detail = array();
        		array_push($a_detail, '<h4 style="padding-margin: 5px;">'.$count.'. Product:'.$h_product['name'].'<br />產品:'.$h_product['cname'].'<br /> '.$h_product['apply_starttime'].'</h4>');
        		/*
				array_push($a_detail, 'Title:'.$h_report['title'].' (Submitted:'.$h_report['addtime'].')'); 
        		if ($h_application != null){
        		  array_push($a_detail, 'Application Status:'.$this->get_application_status($h_application['application_status'])); 
        		}
        		*/
        		array_push($a_result, implode("<BR>", $a_detail));
        		$count++;
        	}
        	
        	if (count($a_result) == 0) return '<H3>No success application.</H3>';

        	return implode("<BR>", $a_result);
        }

/*
> select * from member_condition where mid=6693\G
*************************** 1. row ***************************
                     id: 6693
                    mid: 6693
               interest: 5,8,11
                address: dfadf
               homepage: adfadf
             profession: 1
              age_group: 1
                   body: 2
              face_skin: 1
              body_skin: 2
                   hair: 1
               question: 8,11,14,17
         marital_status: 0
                  brand: 
product_layout_category: 2,5,8
           check_status: 3
                 status: 1
                addtime: 2011-09-16 15:52:18
             updatetime: 0000-00-00 00:00:00
               blogPage: NULL
                  phone: 0
               birthday: 1990-01-16
                   code: 
              reportURL: 
                  photo: 

*/        
        
        function member_type_info($mid){
        	$dbh_r =  $this->db_connect('read');
        	
        	#$member_type = db_select_one($dbh_r, 'select member_type from member_imore where id=?', $mid);
        	
        	
        	#$member_type = db_select_one($dbh_r, 'select member_type from member_imore where id=?', $mid);
        	
        	#if ($member_type<1) return '尚未填寫基本資料';
        	
        	$member_imore = db_selectrow_hash($dbh_r, 'select * from member_imore where id=?', $mid);
        	
			
        	$more = db_selectrow_hash($dbh_r, 'select * from member_condition where mid=?', $mid);
        	if ($more == null){
        		return '尚未填寫專員基本資料';
        	}
        	
        	if ($more['address'] == ''){
        		return '尚未填寫(試用專員)基本資料';
        	}
        	
        	
        	
        	$fields = array(
        	  #"<H3>基本資料（試用專員）：</H3>"
        	  array('試用專員', '','',''),
        	   #    label,  fieldname  , lookup_table,
        	  array('出生日期',   'birthday', '',''),  
        	  array('行業', 'profession', 'profession_type','h:label'),
        	  array('身型', 'body', 'body_type','h:label'),
        	  array('臉部膚質', 'face_skin', 'body_skin_type','h:label'),
        	  array('身體膚質', 'body_skin', 'body_skin_type','h:label'),
        	  array('髮質',     'hair',      'hair_type','h:label'),
         	  array('聯絡方法',  'phone',     '',''),        	  
        	  array('郵寄地址',  'address',   '',''),
        	  );
        	  
        	  
        	$expert_fields = array(
        	  array('體驗達人資料','','',''),
        	  array('個人部落格/網頁', 'homepage','', ''),
        	  array('快速審核驗證', 'blogPage','',''),
        	  array('試用評鑑報告','reportURL','',''),
        	  
        	  array('最喜愛的美容品牌', 'brand','brand', 'h:label'),
        	  array('最關注皮膚問題',   'question','question_type', 'h:label'),
        	  array('個人興趣',        'interest','interest_type', 'h:label'),
        	  array('個人半身近照',     'photo', '', 'photo'),
        	);
        	
        	// check if user is 'expert 
        	if ($more['homepage'] != ''){
        		$fields = array_merge($fields, $expert_fields);
        	}

          $info = array();       	
        	
        	foreach ($fields as $field){
        	
        	  $value = '';
        	  
        	  $n_element = count($field);
        	  
        	  $label = ''; $field_name = ''; $lookup_table=''; $function='';
        	/*  
        	  if ($n_element >= 1)$label =$field[0];
        	  if ($n_element >= 2)$field_name =$field[1];
        	  if ($n_element >= 3)$lookup_table =$field[2];
        	  if ($n_element >= 4)$function =$field[3];
        	  */
        	  
        	  list($label, $field_name, $lookup_table, $function) = $field;
        	  if ($label != '' && $field_name ==''){
        	     array_push($info, '<H3>'.$label.'</h3>');
        	     continue;
        	  }
        	  
        	  if ($lookup_table != ''){
        	  	if ($function == 'h:label'){
        	  		$value = $this->get_lookup_value($lookup_table, $more[$field_name]);
        	  	} 
        	  	
        	  } else {
        	  	if ($function != ''){
        	  		call_user_func_array(array($this, $function), array($value));
        	  	} else {        	  		
        	  	  $value = $more[$field_name];
				  if($value == ''){
					$value = $member_imore[$field_name];
				  }
        	    }
        	    
        	  }
        	  
         	  array_push($info, '<B>'.$label.'</B>'.": ".$value);
        	  
          }
        	
        	 return implode('<BR>', $info);
        }
     
       function get_lookup_value($lookup_table, $value){
       	 $dbh_r =  $this->db_connect('read');
        
         $a_value = explode(',', $value);
         if (count($a_value)>1){
         	$a_result = array();
           foreach ($a_value as $temp){	
             array_push($a_result, db_select_one($dbh_r, 'select cname from '.$lookup_table.' where id=?', $temp));
             
           }
             return implode(',', $a_result);
         } else {
             $value = db_select_one($dbh_r, 'select cname from '.$lookup_table.' where id=?', $value);
			 return $value;
         }
       }
     
       function photo($photo){
        	$image2='';
        	$size1='';
        	$size2='';
        	
        	if ($photo == ''){
        		return 'No Photo';
        	}
        	
        	
        	$html  = '<script type="text/javascript">';
        	$html .= "CMS.image_uplaod.show('$photo','$image2',";
        	$html .= "'$size1','$size2'); </script>";
        	return $html;
        }
      
        function scan_member_application(){
        	$dbh_r = $this->db_connect('read');
          $dbh_w = $this->db_connect('write');
  

          $ids= db_selectcol_array($dbh_r, 'select id from '.$this->param('table').' where status=1 and 
             expert_approval >=1 and expert_approval <=3');

          $count = 0;
          $new_count = 0;
          foreach ($ids as $id){
  	        $count++;
  	        $count_more = db_select_one($dbh_r, 'select count(*) from member_condition where mid=?', $id);
  	        if ($count_more > 0){
  		      $h_more =  db_selectrow_hash($dbh_r, 'select * from member_condition where mid=?', $id);
  		      if ($h_more['homepage'] != '' && $h_more['address'] != ''){
  			    db_do($dbh_w, 'update '.$this->param('table').' set expert_approval=4 where id=?', $id);
  			    $new_count++;
  		   }
  		   return $new_count;  
  	   }
  	
  }
        }
      
		function get_report_info( $report ){

		  $helper = getModel('Model_CMS_Helper');

		  $dbh_r = $this->db_connect('read');
		  $h_report = db_selectrow_hash($dbh_r, 'select * from product_reports where id=?', $report);
		  $report_oid = $h_report['oid'];

		  $h_product =  db_selectrow_hash($dbh_r, 'select * from product where id=?', $report_oid);
		  
		$a_info = array();
		   array_push($a_info,  'Report Title:'.$h_report['title']);
		   array_push($a_info,  $helper->single_photo($h_product['cover_image1']));
		   array_push($a_info,  'Product:');
		   array_push($a_info,  $h_product['name']);
		   array_push($a_info,  $h_product['cname']);
		   array_push($a_info,  'Report:');
		   array_push($a_info,  stripslashes($h_report['body']));

		  return  implode('<BR>', $a_info);  
		 
		}
	  
      
    }
    
    ?>
